import Moment from 'moment'

const Mall = {
  state: {
    mallId: null,
    mallStartTime: null,
    mallEndTime: null,
    mallMapData: [],
    mallMapScale: {},
    mallMapBg: null,
    currMallNotifications: [],
    filterMallNotifications: [],
    // 客流信息 ======
    todayMallCustomerStats: null, // 当天的店铺顾客统计
    yesterdayMallCustomerStats: null, // 昨天的店铺顾客统计
    currCustomerFlowDate: null, // 当前客流数据的时间
    currPassengerTotal: null, // 当前过客总人数
    currCustomerTotal: null, // 当前客流总人数
    currCustomerTotalPeak: null, // 当前客流峰值
    currGenderMaleTotal: null, // 当前男客户总数
    currGenderFemaleTotal: null, // 当前女客户总数
    currCustomerStayTime: null, // 当前停留总时间
    currCustomerTimes: null, // 进店顾客人次（不按人去重
    currDealedTimes: null, // 购买成交次数
    // 热区信息 ======
    currHeatZoneID: null, // 当前热区id
    currHeatZoneInfo: null, // 当前热区信息
    currHeatZoneTotal: null, // 当前热区人数
    // 客流条形图数据 ======
    customerBarData: null, // 客流条形图数据
    // 热区数据 ======
    heatMapData: {}, // 热区数据图
    // 区域流量条形图数据 ======
    areaFlowBarData: null, // 客流条形图数据
    // 区域客流量曲线图数据 ======
    areaCustomerLineData: null,
    // 门店监控列表 ======
    monitoringPlayList: [],
    // 动线图数据
    moveLineData: [], // 客流条形图数据
    yesterdayMinutelyCustomerData: {}
  },

  mutations: {
    SET_OPENNING_HOURS: (state, data) => {
      state.mallStartTime = data ? Number(data[0]) : null
      state.mallEndTime = data ? Number(data[1]) + 1 : null
    },
    SET_MALL_MAP_DATA: (state, data) => {
      state.mallMapData = data
    },
    SET_MALL_MAP_SCALE: (state, data) => {
      state.mallMapScale = data
    },
    SET_MALL_MAP_BG: (state, data) => {
      state.mallMapBg = data
    },
    SET_CURRCUSTOMER_DATA: (state, data) => {
      state.currCustomerFlowDate = data.stats_date
      state.currPassengerTotal = data.customer_num_all
      state.currCustomerTotal = data.customer_num
      state.currCustomerTotalPeak = data.customer_num_peak
      state.currGenderMaleTotal = data.gender_male
      state.currGenderFemaleTotal = data.gender_female
      state.currCustomerStayTime = data.seconds
      state.currCustomerTimes = data.customer_times
      state.currDealedTimes = data.dealed_times
    },
    SET_HEATZONE_DATA: (state, data) => {
      state.currHeatZoneID = data ? data.zone_id : null
      state.currHeatZoneTotal = data ? data.customer_num : null
      state.currHeatZoneInfo = data ? data.zone : null
    },
    SET_CUSTOMERBAR_DATA: (state, data) => {
      state.customerBarData = data
    },
    SET_HEATMAP_DATA: (state, data) => {
      Object.assign(state.heatMapData, data)
    },
    SET_AREAFLOW_DATA: (state, data) => {
      state.areaFlowBarData = data
    },
    SET_AREACUSTOMER_DATA: (state, data) => {
      state.areaCustomerLineData = data
    },
    SET_MOVELINE_DATA: (state, data) => {
      state.moveLineData = data
    },
    SET_CURRMALL_ID: (state, id) => {
      state.mallId = id
    },
    SET_CURRCUSTOMERTOTAL_TOTAL: (state, num) => {
      state.currCustomerTotal = num
    },
    SET_CURRMALLNOTIFICATIONS: (state, list) => {
      state.currMallNotifications = list
    },
    UNSHIFT_CURRMALLNOTIFICATION: (state, msg) => {
      state.currMallNotifications.unshift(msg)
    },
    FILTER_CURRMALLNOTIFICATIONS: (state) => {
      const notification = [...state.currMallNotifications]
      state.filterMallNotifications = notification.filter(value => Moment(value.created_at).unix() >= Moment().subtract(30, 'minutes').unix())
    },
    SET_MALLMONITORING_LIST: (state, list) => {
      state.monitoringPlayList = list
    },
    SET_TODAYCUSTOMER_STATS: (state, stats) => {
      state.todayMallCustomerStats = stats
    },
    SET_YESTERDAYCUSTOMER_STATS: (state, stats) => {
      state.yesterdayMallCustomerStats = stats
    },
    SET_YESTERDAY_MINUTELY_CUSTOMER_DATA: (state, stats) => {
      state.yesterdayMinutelyCustomerData = stats
    }
  },

  actions: {
    setOpenningHours ({ commit }, data) {
      if (data) {
        commit('SET_OPENNING_HOURS', data)
      }
    },
    setMallMapData ({ commit }, data) {
      if (data) {
        commit('SET_MALL_MAP_DATA', data)
      }
    },
    setMallMapScale ({ commit }, data) {
      if (data) {
        commit('SET_MALL_MAP_SCALE', data)
      }
    },
    setMallMapBg ({ commit }, data) {
      if (data) {
        commit('SET_MALL_MAP_BG', data)
      }
    },
    commitCurrCustomerData ({ commit }, data) {
      if (data) {
        commit('SET_CURRCUSTOMER_DATA', data)
      }
    },
    commitCurrHeatZoneData ({ commit }, data) {
      commit('SET_HEATZONE_DATA', data)
    },
    commitCustomerData ({ commit }, data) {
      commit('SET_CUSTOMERBAR_DATA', data)
    },
    commitHeatMapData ({ commit }, data) {
      commit('SET_HEATMAP_DATA', data)
    },
    commitAreaFlowData ({ commit }, data) {
      commit('SET_AREAFLOW_DATA', data)
    },
    commitAreaCustomerData ({ commit }, data) {
      commit('SET_AREACUSTOMER_DATA', data)
    },
    commitMoveLineData ({ commit }, data) {
      commit('SET_MOVELINE_DATA', data || [])
    },
    setmallId ({ commit }, id) {
      commit('SET_CURRMALL_ID', id)
    },
    commitNotificationsList ({ commit }, list) {
      commit('SET_CURRMALLNOTIFICATIONS', list)
    },
    unshiftNotificationsList ({ commit }, msg) {
      commit('UNSHIFT_CURRMALLNOTIFICATION', msg)
    },
    commitMallMonitoringList ({ commit }, list) {
      commit('SET_MALLMONITORING_LIST', list)
    },
    commitCurrCustomerTotal ({ commit }, num) {
      commit('SET_CURRCUSTOMERTOTAL_TOTAL', num)
    },
    // action 提交今日客流数据
    commitTodayCustomerStats ({ commit }, stats) {
      commit('SET_TODAYCUSTOMER_STATS', stats)
    },
    // action 提交昨日客流数据
    commitYesterdayCustomerStats ({ commit }, stats) {
      commit('SET_YESTERDAYCUSTOMER_STATS', stats)
    }
  }
}

export default Mall
